/**
 * 电影列表页面
 */
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'detail.dart';

Dio dio = new Dio();

class MovieList extends StatefulWidget {
  final String mt;

  // 固定写法 MovieList({Key key, this.mt}):super(key: key)
  MovieList({Key key, @required this.mt}) : super(key: key);

  @override
  _MovieListState createState() {
    return new _MovieListState();
  }
}

class _MovieListState extends State<MovieList> {
  int page = 1;

  int pagesize = 10;

  var mlist = [];

  var total = 0;

  @override
  void initState() {
    super.initState();
    getMovieList();
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: mlist.length,
        itemBuilder: (BuildContext ctx, int i) {
          var mitem = mlist[i];
          return GestureDetector(
            onTap: () {
              Navigator.push(context,
                  MaterialPageRoute(builder: (BuildContext ctx) {
                return MovieDetail(
                  id: mitem['id'],
                  title: mitem['title'],
                );
              }));
            },
            child: Container(
              padding: EdgeInsets.only(
                  top: 5.0, bottom: 5.0, left: 10.0, right: 10.0),
              height: 200.0,
              decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border(
                      top: BorderSide(width: 1.0, color: Colors.black12))),
              child: Row(
                children: <Widget>[
                  Image.network(mitem['images']['small'],
                      width: 130.0, height: 200.0, fit: BoxFit.cover),
                  Container(
                    padding: EdgeInsets.only(left: 15.0),
                    height: 180.0,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text('电影名称: ${mitem['title']}'),
                        Text('上映年份: ${mitem['year']}'),
                        Text('电影类型: ${mitem['genres'].join('，')}'),
                        Text('豆瓣评分: ${mitem['rating']['average']}'),
                        Text('主要演员: ${mitem['title']}')
                      ],
                    ),
                  )
                ],
              ),
            ),
          );
        });
  }

  getMovieList() async {
    int offset = (page - 1) * pagesize;

    var response = await dio.get(
        'http://www.liulongbin.top:3005/api/v2/movie/${widget.mt}?start=$offset&count=$pagesize');
    var result = response.data;

    setState(() {
      mlist = result['subjects'];
      total = result['total'];
    });
  }
}
